﻿/*
 * Copyright (c) HiSilicon (Shanghai) Technologies Co., Ltd. 2023-2023. All rights reserved.
 */

#ifndef __READHEX_H__
#define __READHEX_H__

#include <cstdio>
#include <cstdint>
#include <QDebug>
#include <iostream>

using off64_t = long long int;

#ifdef __cplusplus
extern "C" {
#endif

typedef void (*MemoryFunc)(void *arg, int address, unsigned char c);
int ReadHex(FILE *f, MemoryFunc memory, void *memoryArg, int badChecksumIsError);
bool GetLineAddress(uint8_t **s);
void RecodeData(MemoryFunc memory, void *memoryArg, uint8_t **s);
void RecordStartLinearAddress(int64_t &startExecution, uint8_t &checksum, uint8_t **s);
void RecordExtendedLinearAddress(int &startAddress, int &endAddress, int &dataOffset, uint8_t &checksum, uint8_t **s);
void RecordStartSegMentAddress(int32_t &startSegmentCs, int32_t &startSegmentIp, uint8_t &checksum, uint8_t **s);
void RecordExtendedSegMentAddress(int baseAddress, uint8_t &checksum, uint8_t **s);
int VerifyChecksum(uint8_t &checksum, uint8_t *s, int badChecksumIsError);

namespace MemoryClass {
struct MemoryDesc {
    unsigned char *p;
    off64_t offset;
    size_t size;
    size_t sizeWritten;
};

typedef struct MemoryAddresses {
    int *addresses;
    int count;
} MemoryAddresses;
}
void MemoryDescStore(void *arg, int address, uint8_t c);
void MemoryAddressesStore(void *arg, int address, unsigned char c);

#ifdef __cplusplus
}
#endif

#endif // __READHEX_H__
